package rejjie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import rejjie.common.CustomException;
import rejjie.common.R;
import rejjie.entity.Category;
import rejjie.entity.Dish;
import rejjie.entity.Setmeal;
import rejjie.service.CategoryService;
import rejjie.service.DishService;
import rejjie.service.SetmealService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;




    /**
     * 对菜品分类和套餐分类进行插入
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * 进行分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        //分页构造器
        Page<Category> pageinfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        //添加排序条件，根据 顺序字段sort进行排序
       lambdaQueryWrapper.orderByAsc(Category::getSort);
        //分页查询
        categoryService.page(pageinfo,lambdaQueryWrapper);

        return R.success(pageinfo);
    }

    /**
     * 根据id进行删除
     * 因为用 dish,setmeal中有相关的数据（菜品id 分类id） 所以我们不能够随意删除，如果我们删除需要对他们进行查询
     * 如果有相关的数据那么我们就不能进行删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("得到的菜品id{} ",ids);

        categoryService.remove(ids);
        return R.success("分类信息删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }

    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> List(Category category){
        
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return R.success(list);
    }

}
